package com.sg.business.commons.ui.work;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;

import com.mobnut.db.model.IContext;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.commons.field.IValueChangedListener;

public abstract class EngineeringChangePanel extends Composite {

	private int controlStyle;

	private EngineeringChangeWorkListCreater workListCreater;
	private EngineeringChangeProductScopeCreater productChangeScope;
	private EngineeringChangeBusinessScopeCreater businessChangeScope;
	private EngineeringChangeNoticeCreater changeNotice;

	private PrimaryObject input;

	private boolean enabled;

	public static int WORKLIST_CREATER = 9 << 1;

	public static int PRODUCT_CREATER = 9 << 2;

	public static int BUSINESS_CREATER = 9 << 3;

	public static int NOTICE_CREATER = 9 << 4;

	public EngineeringChangePanel(Composite parent, int controlStyle) {
		super(parent, SWT.NONE);
		this.controlStyle = controlStyle;
	}

	private void initData() {
		if (hasWorkListCreater()) {
			workListCreater.setInput(input);
		}

		if (hasProductCreater()) {
			productChangeScope.setInput(input);
			if (hasWorkListCreater()) {
				workListCreater
						.addValueChangeListener(new IValueChangedListener() {

							@Override
							public void valueChanged(Object newValue) {
								productChangeScope.setInput(input);
							}
						});
			}
		}

		if (hasBusinessCreater()) {
			businessChangeScope.setInput(input);
		}

		if (hasNoticeCreater()) {
			changeNotice.setInput(input);
		}
	}

	public void createContent() {
		setLayout(new FormLayout());

		Composite bottom = null;

		bottom = createChangeContext(bottom);

		bottom = createProductChangeScope(bottom);

		bottom = createBusinessChangeScope(bottom);

		createChangeNoticeScope(bottom);
	}

	private Composite createChangeNoticeScope(Composite bottom) {
		if (hasNoticeCreater()) {
			changeNotice = new EngineeringChangeNoticeCreater(this, SWT.NONE);
			changeNotice.setContext(getContext());
			changeNotice.setCanEnabled(enabled && canEnableNoticeCreater());
			changeNotice.createContent();

			FormData fd = new FormData();
			changeNotice.setLayoutData(fd);
			fd.left = new FormAttachment(0, 2);
			if (bottom != null) {
				fd.top = new FormAttachment(bottom, 2);
			} else {
				fd.top = new FormAttachment(0, 2);
			}
			fd.right = new FormAttachment(100, -2);
			fd.height = 285;
			return changeNotice;
		}
		return bottom;
	}

	private Composite createBusinessChangeScope(Composite bottom) {
		if (hasBusinessCreater()) {
			businessChangeScope = new EngineeringChangeBusinessScopeCreater(
					this, SWT.NONE);
			businessChangeScope.setContext(getContext());
			businessChangeScope.setCanEnabled(enabled
					&& canEnableBusinessCreater());
			businessChangeScope.createContent();

			FormData fd = new FormData();
			businessChangeScope.setLayoutData(fd);
			fd.left = new FormAttachment(0, 2);
			if (bottom != null) {
				fd.top = new FormAttachment(bottom, 2);
			} else {
				fd.top = new FormAttachment(0, 2);
			}
			fd.right = new FormAttachment(100, -2);
			fd.height = 305;
			return businessChangeScope;
		}
		return bottom;
	}

	private Composite createProductChangeScope(Composite bottom) {
		if (hasProductCreater()) {
			productChangeScope = new EngineeringChangeProductScopeCreater(this,
					SWT.NONE);
			productChangeScope.setContext(getContext());
			productChangeScope.setCanEnabled(enabled
					&& canEnableProductCreater());
			productChangeScope.createContent();

			FormData fd = new FormData();
			productChangeScope.setLayoutData(fd);
			fd.left = new FormAttachment(0, 2);
			if (bottom != null) {
				fd.top = new FormAttachment(bottom, 2);
			} else {
				fd.top = new FormAttachment(0, 2);
			}
			fd.right = new FormAttachment(100, -2);
			fd.height = 270;
			return productChangeScope;
		}
		return bottom;
	}

	private Composite createChangeContext(Composite bottom) {
		if (hasWorkListCreater()) {
			workListCreater = new EngineeringChangeWorkListCreater(this,
					SWT.NONE);
			workListCreater.setContext(getContext());
			workListCreater
					.setCanEnabled(enabled && canEnableWorkListCreater());
			workListCreater.createContent();

			FormData fd = new FormData();
			workListCreater.setLayoutData(fd);
			fd.left = new FormAttachment(0, 2);
			fd.top = new FormAttachment(0, 2);
			fd.right = new FormAttachment(100, -2);
			fd.height = 255;

			return workListCreater;
		}
		return bottom;
	}

	private boolean hasWorkListCreater() {
		return (controlStyle & WORKLIST_CREATER) != 0;
	}

	private boolean hasProductCreater() {
		return (controlStyle & PRODUCT_CREATER) != 0;
	}

	private boolean hasBusinessCreater() {
		return (controlStyle & BUSINESS_CREATER) != 0;
	}

	private boolean hasNoticeCreater() {
		return (controlStyle & NOTICE_CREATER) != 0;
	}

	protected abstract IContext getContext();

	protected boolean canEnableNoticeCreater() {
		return true;
	}

	protected boolean canEnableBusinessCreater() {
		return true;
	}

	protected boolean canEnableProductCreater() {
		return true;
	}

	protected boolean canEnableWorkListCreater() {
		return true;
	}

	public void setInput(PrimaryObject data) {
		this.input = data;
		initData();
	}

	public PrimaryObject getInput() {
		return input;
	}

	public void setCanEnabled(boolean enabled) {
		this.enabled = enabled;
	}
}
